package com.erp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.ReplyDAO;
import com.erp.dao.TopicDAO;
import com.erp.domain.TReply;
import com.erp.domain.TTopic;
import com.erp.dto.Page;
import com.erp.dto.Reply;
import com.erp.mapping.Status;

@Service(value = "replyService")
@Transactional
public class ReplyService {

	@Resource(name = "replyDAO")
	private ReplyDAO replyDAO;
	@Resource(name = "topicDAO")
	private TopicDAO topicDAO;

	public boolean add(Reply reply) {
		TTopic tTopic = topicDAO.getById(reply.getTopicId());
		// 设置主题的回复数加1
		tTopic.setReplyCount(tTopic.getReplyCount() + 1);

		// 设置主题的最大楼层加1
		int maxFloor = tTopic.getMaxFloor();
		tTopic.setMaxFloor(tTopic.getMaxFloor() + 1);

		tTopic.setLastUpdateTime(reply.getPostTime());
		TReply tReply = new TReply(reply);
		// 设置回复所在楼层
		tReply.setFloor(maxFloor + 1);

		tReply.settTopic(tTopic);
		replyDAO.save(tReply);
		tTopic.setLasttReply(tReply);
		return true;
	}

	/**
	 * (在回收站里)彻底删除回复，版块显示的回复数量不变
	 * 
	 * @param model
	 */
	public boolean delete(Reply reply) {
		replyDAO.deleteById(reply.getId());
		return true;
	}

	/**
	 * 添加已删除标记，版块显示的回复数量-1
	 * 
	 * @param topic
	 * @return
	 */
	public boolean setStatusDeleted(Reply reply) {
		TReply tReply = replyDAO.getById(reply.getId());
		tReply.setStatus(Status.STATUS_DELETED);// 设为已删除
		TTopic tTopic = tReply.gettTopic();
		tTopic.setReplyCount(tTopic.getReplyCount() - 1);// 主题的回复数-1
		return true;
	}

	/**
	 * 将回复状态恢复为正常，版块显示的回复数量+1
	 * 
	 * @param forum
	 * @return
	 */
	public boolean setStatusNormal(Reply reply) {
		TReply tReply = replyDAO.getById(reply.getId());
		tReply.setStatus(Status.STATUS_NORMAL);// 恢复为正常状态
		TTopic tTopic = tReply.gettTopic();
		tTopic.setReplyCount(tTopic.getReplyCount() + 1);// 主题的回复数+1
		return true;
	}

	public List<Reply> listByTopicId(Reply reply) {
		List<Reply> replyList = new ArrayList<Reply>();
		List<TReply> tReplyList = new ArrayList<TReply>();
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("tTopic", topicDAO.getById(reply.getTopicId()));
		tReplyList = replyDAO.list(
				"from TReply reply where reply.tTopic=:tTopic  and reply.status="
						+ Status.STATUS_NORMAL.ordinal()
						// 前提：置顶类型序号比普通类型大
						+ " order by reply.postTime", mapping);

		if (tReplyList != null) {
			for (TReply tReply : tReplyList) {
				replyList.add(new Reply(tReply));
			}
		}
		return replyList;
	}

	public List<Reply> pagedListByTopicId(Reply reply, Page page) {
		// 开始页码
		int from = (page.getPageIndex() - 1) * page.getPageSize();
		String hql = "from TReply reply where reply.tTopic=:tTopic  and reply.status="
				+ Status.STATUS_NORMAL.ordinal()
				// 前提：置顶类型序号比普通类型大
				+ " order by reply.postTime";
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("tTopic", topicDAO.getById(reply.getTopicId()));
		List<Reply> replyList = null;
		int totalRecord = replyDAO.count("select count(*) " + hql, mapping)
				.intValue();
		page.setTotalRecord(totalRecord);
		int totalPage = 1;
		if (totalRecord > 0) {
			replyList = new ArrayList<Reply>();
			List<TReply> tReplyList;
			tReplyList = replyDAO
					.paging(hql, mapping, from, page.getPageSize());
			// 设置总页数
			totalPage = totalRecord % (page.getPageSize()) > 0 ? totalRecord
					/ (page.getPageSize()) + 1 : totalRecord
					/ (page.getPageSize());

			for (int i = 0; i < tReplyList.size(); i++) {
				replyList.add(new Reply(tReplyList.get(i)));
			}
		}
		page.setTotalPage(totalPage);
		return replyList;
	}

	/**
	 * 获取回复回收站列表
	 * 
	 * @return
	 */
	public List<Reply> getRecycleList() {
		List<Reply> replyRecycleList = new ArrayList<Reply>();
		List<TReply> tReplyRecycleList = new ArrayList<TReply>();
		tReplyRecycleList = replyDAO
				.list("from TReply reply where reply.status="
						+ Status.STATUS_DELETED.ordinal()
						+ " order by reply.postTime desc");

		if (tReplyRecycleList != null) {
			for (TReply tReply : tReplyRecycleList) {
				replyRecycleList.add(new Reply(tReply));
			}
		}
		return replyRecycleList;
	}

}
